import { App } from 'vue';

// 创建一个简单的 uni API 模拟
const uniApi = {
  navigateTo({ url }: { url: string }) {
    if (typeof window !== 'undefined') {
      window.location.hash = url.replace(/^\//, '');
    }
  },
  showToast({ title, icon }: { title: string; icon?: string }) {
    if (typeof window !== 'undefined') {
      alert(title);
    }
  },
  getStorageSync(key: string): any {
    if (typeof window !== 'undefined' && window.localStorage) {
      const value = window.localStorage.getItem(key);
      return value ? JSON.parse(value) : null;
    }
    return null;
  },
  setStorageSync(key: string, data: any): void {
    if (typeof window !== 'undefined' && window.localStorage) {
      window.localStorage.setItem(key, JSON.stringify(data));
    }
  },
  removeStorageSync(key: string): void {
    if (typeof window !== 'undefined' && window.localStorage) {
      window.localStorage.removeItem(key);
    }
  }
};

// 创建 Vue 插件
export const UniPlugin = {
  install(app: App) {
    // 添加全局属性
    app.config.globalProperties.$uni = uniApi;
    
    // 添加全局变量
    window.uni = uniApi;
  }
};

// 导出类型定义
declare global {
  interface Window {
    uni: typeof uniApi;
  }
} 